﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DL_Project_ManageAdvancedUser : PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    TOUserGroup objUserGroup = new TOUserGroup();
    TOAdvancedUser objAUser = new TOAdvancedUser();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int CID = 0;

            
            InitPage();
            //BinderUserGroup();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
            if (CheckUser())
            {
                btnAddNew.Enabled = true;
                btn_DelCheck.Enabled = true;
            }
            else
            {
                btnAddNew.Enabled = false;
                btn_DelCheck.Enabled = false;
            }
        }
    }
    private bool CheckUser()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1 || login.VOAdvancedUser.AUserGroup == 2)
                {
                    rt = true;
                }

            }
 
        }

        return rt;

    }

    private int CheckPopedom()
    {
        int rt = 0;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            rt = login.VOAdvancedUser.AUserGroup;
        }
        return rt;

    }
    ////绑定用户组
    //private void BinderUserGroup()
    //{
    //    string search = "";
    //    //DataTable dt = objUserGroup.Query(search);

    //    //ddl_UserGroup.DataSource = dt;

    //    ListItem li1 = new ListItem();
    //    li1.Text = "系统管理员";
    //    li1.Value = "1";
    //    ddl_UserGroup.Items.Add(li1);
    //    ListItem li2 = new ListItem();
    //    li2.Text = "区域管理员";
    //    li2.Value = "2";
    //    ddl_UserGroup.Items.Add(li2);
    //    ListItem li3 = new ListItem();
    //    li3.Text = "主讲教师";
    //    li3.Value = "3";
    //    ddl_UserGroup.Items.Add(li3);
    //    ListItem li4 = new ListItem();
    //    li4.Text = "班级管理员";
    //    li4.Value = "4";
    //    ddl_UserGroup.Items.Add(li4);
    //    ListItem li5 = new ListItem();
    //    li5.Text = "课程辅导员";
    //    li5.Value = "5";
    //    ddl_UserGroup.Items.Add(li5);

    //    ListItem li6 = new ListItem();
    //    li6.Text = "资源管理员";
    //    li6.Value = "6";
    //    ddl_UserGroup.Items.Add(li6);

    //    ListItem li = new ListItem();
    //    li.Text = "全部";
    //    li.Value = "0";
    //    ddl_UserGroup.Items.Insert(0, li);
    //}


    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerNew.Text ="高级用户管理";
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = " 1=1 ";
        //if (ddl_UserGroup.SelectedValue != "0")
        //    search =search+ " and AUserGroup= " + ddl_UserGroup.SelectedValue;
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            search =search+ string.Format(" and Name like '%{0}%' ", txt_KeyWord.Text.Trim());
        //Response.Write(search);
        //Response.End();
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objAUser.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        string orderby = " AUser_Id desc";
        DataTable dt = objAUser.Query(search, orderby, LMSPageSize, pageIndex);
        //dt.Columns.Add("RoleName");
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    dt.Rows[i]["RoleName"] = objAUser.getUserGroupName(int.Parse(dt.Rows[i]["AUserGroup"].ToString()));
        //}
        rptNew.DataSource = dt;
        rptNew.DataBind();

       // PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            Label lbl_RoleID = (Label)e.Item.FindControl("lbl_RoleID");
            if (CheckUser())
            {
                if (lbl_RoleID.Text == "1")
                {
                    if (CheckPopedom() == 2)
                    {
                        lnkbtn_editor.Enabled = false;
                        lbtn.Enabled = false;

                    }
                    else
                    {
                        lnkbtn_editor.Enabled = true;
                        lbtn.Enabled = true;
                        lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
                    }
                }
                else
                {
                    lnkbtn_editor.Enabled = true;
                    lbtn.Enabled = true;
                    lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
                }
            }
            else
            {
                lnkbtn_editor.Enabled = false;
                lbtn.Enabled = false;
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = " 1=1 ";
        //if (ddl_UserGroup.SelectedValue != "0")
        //    search = search + " and AUserGroup= " + ddl_UserGroup.SelectedValue;
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            search = search + string.Format(" and Name like '%{0}%' ", txt_KeyWord.Text.Trim());
        //Response.Write(search);
        //Response.End();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddAdvancedUser.aspx";
        Script.RedirectURL(url);

        //if (!string.IsNullOrEmpty(txt_KeyWord.Text))
        //{
        //    VOTOProvince vo = new VOTOProvince();
        //    vo.Province = txt_KeyWord.Text.Trim();
        //    vo.CityNum = 0;
        //    obj.Add(vo);

        //}
        //txt_KeyWord.Text = "";
        //string search = GetSearch();
        //BinderPager(search);
        //BinderPG(search, AspNetPager1.CurrentPageIndex);

    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {

            string url = "AddAdvancedUser.aspx?AID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
           
        }
        else if (e.CommandName.ToLower() == "ldel")
        {

            int AID = int.Parse(e.CommandArgument.ToString());
            if (objAUser.Del(AID))
            {
                string search = GetSearch();
                BinderPager(search);
                BinderPG(search, AspNetPager1.CurrentPageIndex);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
           
        }
        
       
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    int AID = int.Parse(lab_NewID.Text);
                    if (!objAUser.Del(AID))
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }

                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }


}

